﻿using System;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;

namespace WindowsFormsApplication1
{
    class TalkServ
    {
       
            private TcpListener tcpListener;
            private Thread listenThread;
            private byte[] lastMessage;
           // private BlackBox blackBox = new BlackBox();

            public TalkServ()
            {
                this.tcpListener = new TcpListener(IPAddress.Any, 23);
                this.listenThread = new Thread(new ThreadStart(ListenForClients));
                this.listenThread.Start();
            }

            private void ListenForClients()
            {
                this.tcpListener.Start();

                while (true)
                {
                    //blocks until a client has connected to the server
                    TcpClient client = this.tcpListener.AcceptTcpClient();
                    Console.WriteLine("connected.. we think");
                    //create a thread to handle communication
                    //with connected client
                    Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                    clientThread.Start(client);
                }
            }

            private void HandleClientComm(object client)
            {
                TcpClient tcpClient = (TcpClient)client;
                NetworkStream clientStream = tcpClient.GetStream();

                byte[] message = new byte[4096];
                int bytesRead;

                while (true)
                {
                    bytesRead = 0;

                    try
                    {
                        //blocks until a client sends a message
                        bytesRead = clientStream.Read(message, 0, 4096);

                    }
                    catch
                    {
                        //a socket error has occured
                        break;
                    }

                    if (bytesRead == 0)
                    {
                        //the client has disconnected from the server
                        break;
                    }

                    //message has successfully been received
                    ASCIIEncoding encoder = new ASCIIEncoding();
                    System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
                    Console.WriteLine(encoder.GetString(message, 0, bytesRead));
                    if (encoder.GetString(message, 0, bytesRead) == "f")
                    {
                        SendMessage(client);
                        
                    }//if
                    else if (encoder.GetString(message, 0, bytesRead) == "W")
                        {
                        SendMessage(client,1);
                        
                    }//if
                    else if (encoder.GetString(message, 0, bytesRead) == "w")
                    {
                        SendMessage(client,0);

                    }//if

                   
                }

                tcpClient.Close();
            }



            public void SendMessage(object client, byte water) //overloaded with water
            {
                TcpClient tcpClient = (TcpClient)client;
                byte[] message = new byte[7] { 255, 255, BlackBox.signedBit, BlackBox.airTempWhole, BlackBox.airTempFract, BlackBox.soilMoisture, water };

                NetworkStream clientStream = tcpClient.GetStream();
                ASCIIEncoding encoder = new ASCIIEncoding();

                clientStream.Write(message, 0, message.Length);
                Console.WriteLine(message[0]);
                clientStream.Flush();

            }
        public void SendMessage(object client)
            {
                TcpClient tcpClient = (TcpClient)client;
                byte[] message = new byte[7] { 255, 255, BlackBox.signedBit,BlackBox.airTempWhole, BlackBox.airTempFract, BlackBox.soilMoisture, BlackBox.waterStatus };

                NetworkStream clientStream = tcpClient.GetStream();
                ASCIIEncoding encoder = new ASCIIEncoding();

                clientStream.Write(message, 0, message.Length);
                Console.WriteLine(message[0]);
                clientStream.Flush();
                
            }
        }//class



}
     